import $ajax from '../utils/http.js';
import check_form from '../utils/reg.js'
//获取元素 

const reg_form = document.querySelector('form');
const username_ele = document.getElementById('username')
const password_ele = document.getElementById('password')
const rpassword_ele = document.getElementById('rpassword')
const nickname_ele = document.getElementById('nickname')











reg_form.addEventListener('submit',async e=>{
    e.preventDefault(); //阻止提交

    const username = username_ele.value;
    const password = password_ele.value;
    const rpassword = rpassword_ele.value;
    const nickname = nickname_ele.value;

    // 组装数据 作为请求体 
    const userInfo= {
        username:username,
        password:password,
        rpassword:rpassword,
        nickname:nickname

    }

    if(!username || !password || !rpassword || !nickname){
        return alert('表单内容不能为空');
    }
    
    if(password !== rpassword){
        return alert('两次密码不一致')
    }
    
    if(!check_form.testName(username) || !check_form.testPass(password) ||!check_form.testNick(nickname)){
        return alert('用户名或者密码或者昵称不符合要求');
    }

    console.log(userInfo);


    const res = await $ajax.post('/users/register',userInfo)

    if(res.code !== 1){
        return alert(res.message);
    }

    alert('注册成功,马上跳转到登录页面')
    window.location.href = '../views/login.html';

})









